package com.skyworth.web.admin.user.service.impl;

import com.google.common.collect.Lists;
import com.google.gson.JsonObject;
import com.skyworth.web.common.Constants;
import com.skyworth.web.admin.user.domain.UserKind;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.admin.user.form.UserForm;
import com.skyworth.web.admin.user.repository.UserRepository;
import com.skyworth.web.admin.user.service.UserService;
import com.skyworth.web.wechat.common.MD5Encode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserRepository userRepository;

    /**
     *
     * @param username 用户名称，id或者手机
     * @return
     */
    @Override
    public UserModel findUserByUsername(String username) {
        return userRepository.findByUsername(username);
    }

    @Override
    public UserModel findUserByMobile(String mobile) {
        return userRepository.findByMobile(mobile);
    }

    @Override
    public UserModel findUserByEmployeeNo(String employeeNo) {
        return userRepository.findByEmployeeNo(employeeNo);
    }

    @Override
    public Page<UserModel> userPage(JsonObject params) {
        PageRequest request = new PageRequest(0, Constants.PAGE_SIZE, Sort.Direction.DESC, "id");
        return userRepository.users(request);
    }

    @Override
    public Page<UserModel> userPage(JsonObject params,Pageable pageable) {
        if(params != null){
            long storeId = params.get("store_id").getAsLong();
            return userRepository.findByStoreId(storeId, pageable);
        }
        return userRepository.findAll(pageable);
    }

    @Override
    public boolean existEmployee(String employeeNo, long id, long store_id) {
        return userRepository.existEmployee(employeeNo, id, store_id);
    }

    @Override
    public boolean existUser(String username, long id, long store_id) {
        return userRepository.existUser(username, id, store_id);
    }

    @Override
    public boolean existPhone(String mobile, long id, long store_id) {
        return userRepository.existPhone(mobile, id, store_id);
    }

    @Override
    public boolean existsByUsername(String username) {
        return userRepository.existsByUsername(username);
    }

    @Override
    public boolean existsByMobile(String mobile) {
        return userRepository.existsByMobile(mobile);
    }

    @Override
    public boolean existsByMobile(String mobile , long store_id) {
        return userRepository.existsByMobile(mobile, store_id);
    }

    @Override
    public boolean existsByUsername(String username, long store_id) {
        return userRepository.existsByUsername(username,store_id);
    }

    @Override
    public boolean existsByEmployeeNo(String employeeNo, long store_id) {
        return userRepository.existsByEmployeeNo(employeeNo,store_id);
    }

    @Override
    public UserModel addUser(UserForm f) {
        UserModel user = new UserModel();
        user.employeeNo = f.employeeNo;
        user.username = f.username;
        user.mobile = f.mobile;
        user.password = MD5Encode.encode(f.password);
        user.storeId = f.storeId;
        user.roleId = f.roleId;
        user.kind = UserKind.initWithCode(f.kind);
        user.accessToken = java.util.UUID.randomUUID().toString();
        userRepository.save(user);

        return user;
    }

    @Override
    public UserModel updateUser(UserForm f) {
        UserModel user = this.findUser(f.id);
        user.username = f.username;
        user.mobile = f.mobile;
        user.employeeNo = f.employeeNo;

        if (f.password != null && !f.password.isEmpty()) {
            user.password = MD5Encode.encode(f.password);
        }
        user.kind = UserKind.initWithCode(f.kind);
        user.roleId = f.roleId;
        userRepository.save(user);

        return user;
    }

    @Override
    public UserModel updateUser(UserModel userModel) {
        userRepository.save(userModel);
        return userModel;
    }

    @Override
    public void setStoreId(long id, long storeId) {
         userRepository.updateStoreId(id, storeId);
    }

    @Override
    public UserModel findUser(long id) {
        return userRepository.findOne(id);
    }

    @Override
    public void removeUser(long id) {
        userRepository.delete(id);
    }

    @Override
    public List<UserModel> userList(JsonObject params) {
        if(null != params && null != params.get("store_id")) {
            return userRepository.findByStoreId(params.get("store_id").getAsLong());
        }else {
            return Lists.newArrayList(userRepository.findAll());
        }
    }

    @Override
    public List<UserModel> waiterList(long storeId) {
        return userRepository.findWaiterByStoreId(storeId);
    }

    @Override
    public UserModel findUserByAccessToken(String accessToken) {
        return userRepository.findByAccessToken(accessToken);
    }

}
